
var mapIsInit = false;
//地图初始化完成时 回调
function MapReady() {
    mapIsInit = true;
    console.log("地图初始化完成 2");
    //JsSetInitView();
}

/// 以下代码用于 视角控制-------------------------------------

//设置初始化视角 参数数量跟据需要自行调整，数量为2个至5个，用逗号进行间隔。顺序为=>经度,纬度,视离,朝向,抑角
//需要在MapReady回调函数中进行调用才有效，只有0.5秒的设置时间
function JsSetInitView() {
    var tx = 108.34935;//经度
    var ty = 22.92365;//纬度
    var zoom = 600;//摄像机与视觉中心点距离 单位（米）
    var rot = 180;//朝向角度 (正北为0度，正南为180度)
    var pitch = -50;//抑视角度（数值范围0至-85，垂直视角为0)
    var parm = tx+","+ty+","+zoom+","+rot+","+pitch; //参数数量跟据需要自行调整，数量为2个至5个，用逗号进行间隔。顺序为=>经度,纬度,视离,朝向,抑角
    unity.SendMessage("Map", "JsSetInitView", parm);
}

//跳转到视角 参数数量跟据需要自行调整，数量为2个至5个，用逗号进行间隔。顺序为=>经度,纬度,视离,朝向,抑角
function JsFly() {
    var tx = 108.34935;//经度
    var ty = 22.92365;//纬度
    var zoom = 400;//摄像机与视觉中心点距离 单位（米）
    var rot = 60;//朝向角度 (正北为0度，正南为180度)
    var pitch = -30;//抑视角度（数值范围0至-85，垂直视角为0)
    var parm = tx+","+ty+","+zoom+","+rot+","+pitch; //参数数量跟据需要自行调整，数量为2个至5个，用逗号进行间隔。顺序为=>经度,纬度,视离,朝向,抑角
    //var parm = tx+","+ty+","+zoom;  //例如可以这样使用参数
    //var parm = tx+","+ty;           //例如可以这样使用参数
    unity.SendMessage("Map", "JsFly", parm);
}

//跳转到目标对象位置 根据GID查找  参数数量跟据需要自行调整，数量为1个至4个，用逗号进行间隔。顺序为=>GID,视离,朝向,抑角
function JsFlyByGID() {
    var gid = ""
    var zoom = 400;//摄像机与视觉中心点距离 单位（米）
    var rot = 180;//朝向角度 (正北为0度，正南为180度)
    var pitch = -30;//抑视角度（数值范围0至-85，垂直视角为0)
    var parm = gid+","+zoom+","+rot+","+pitch; //参数数量跟据需要自行调整，数量为1个至4个，用逗号进行间隔。顺序为=>经度,纬度,视离,朝向,抑角
    //var parm = gid+","+zoom;  //例如可以这样使用参数
    //var parm = gid;           //例如可以这样使用参数
    unity.SendMessage("Map", "JsFlyByGID", parm);
}


////


//以下代码用于 外接画线和js调用扩展

function showDraw() {
    var obj = { gid: "line01" };
    obj.coordList = [];
    obj.coordList[0] = {lng_tx:108.351702738998,lat_ty:22.9217816435415,dem:1.5};
    obj.coordList[1] = {lng_tx:108.352258228109,lat_ty:22.9223579274256,dem:1.5};
    obj.coordList[2] = {lng_tx:108.351909520453,lat_ty:22.9223229159115,dem:1.5};
    obj.coordList[3] = {lng_tx:108.351848976743,lat_ty:22.9228141762385,dem:1.5};
    obj.pointSize = 0.5;
    obj.lineSize = 0.4;
    obj.color = "#00ff00";
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsDraw", json);
    }
}
function changeDraw() {
    var obj = { gid: "line01" };
    obj.coordList = [];
    obj.coordList[0] = {lng_tx:108.351702738998,lat_ty:22.9217816435415,dem:1.5};
    obj.coordList[1] = {lng_tx:108.352258228109,lat_ty:22.9223579274256,dem:1.5};
    obj.coordList[2] = {lng_tx:108.351909520453,lat_ty:22.9223229159115,dem:1.5};
    obj.coordList[3] = {lng_tx:108.351848976743,lat_ty:22.9228141762385,dem:1.5};
    obj.coordList[4] = {lng_tx:108.352113915386,lat_ty:22.9231303205913,dem:1.5};
    obj.pointSize = 0.5;
    obj.lineSize = 1;
    obj.color = "#00ff00";
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsDraw", json);
    }
}
function changeDrawAutoDem() {
    var obj = { gid: "line01" };
    obj.coordList = [];
    obj.coordList[0] = {lng_tx:108.351702738998,lat_ty:22.9217816435415,dem:1.5};
    obj.coordList[1] = {lng_tx:108.352258228109,lat_ty:22.9223579274256,dem:1.5};
    obj.coordList[2] = {lng_tx:108.351909520453,lat_ty:22.9223229159115,dem:1.5};
    obj.coordList[3] = {lng_tx:108.351848976743,lat_ty:22.9228141762385,dem:1.5};
    obj.coordList[4] = {lng_tx:108.352113915386,lat_ty:22.9231303205913,dem:1.5};
	obj.autoDem=true;
    obj.pointSize = 0.5;
    obj.lineSize = 1;
    obj.color = "#00ff00";
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsDraw", json);
    }
}

///   以下代码用于  js调用扩展画线填充
function showDraw() {
    var obj = { gid: "fill01" };
	obj.isClose = true;//是否为封闭填充
    obj.coordList = [];
    obj.coordList[0] = {lng_tx:108.351275075087,lat_ty:22.9239935292536,dem:1.229};
    obj.coordList[1] = {lng_tx:108.351107593858,lat_ty:22.9245244937963,dem:1.229};
    obj.coordList[2] = {lng_tx:108.351648474077,lat_ty:22.9246951027093,dem:1.229};
    obj.coordList[3] = {lng_tx:108.351815956531,lat_ty:22.9241641405765,dem:1.229};
    obj.lineColor = "#0000ff";
    obj.fillColor = "#0000ff";
    if (mapIsInit) {
        var json = JSON.stringify(obj);
		console.log(json);
        unity.SendMessage("Map", "JsDraw", json);
    }
}
function delectDraw() {
    if (mapIsInit) {
        unity.SendMessage("Map", "JsDelDraw", "fill01");
    }
}
function showTips() {
    var obj = { gid: "p01" };
    obj.coord = { lng_tx: 113.266613476218, lat_ty: 23.195843688078, dem: 0.6 };
    obj.tips = { label: "定位A" };
    obj.tips.urlTipsFill = "preview/tips/tips_blue.png";
    obj.tips.urlTipsIcon = "preview/tips/tips_icon_2.png";
    obj.tips.height = 10.0;
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsShowObj", json);
    }
}
function showTipsArray() {
    var obj = { gid: "p01" };
    obj.coord = { lng_tx:113.266613476218, lat_ty: 23.195843688078, dem: 0.6 };
    obj.tips = { label: "定位A" };
    obj.tips.urlTipsFill = "preview/tips/tips_blue.png";
    obj.tips.urlTipsIcon = "preview/tips/tips_icon_2.png";
    obj.tips.height = 10.0;
	
    var objB = { gid: "p02" };
    objB.coord = { lng_tx:113.266413476218, lat_ty: 23.195843688078, dem: 0.6 };
    objB.tips = { label: "定位B" };
    objB.tips.urlTipsFill = "preview/tips/tips_blue.png";
    objB.tips.urlTipsIcon = "preview/tips/tips_icon_2.png";
    objB.tips.height = 10.0;
    if (mapIsInit) {
        var json = JSON.stringify([obj,objB]);
        unity.SendMessage("Map", "JsShowObjArray", json);
    }
}
function changeTips() {
    var obj = { gid: "p01" };
    obj.coord = { lng_tx: 108.34968, lat_ty: 22.9236, dem: 1.3 };
    obj.tips = { enable: true, label: "定位" };
    obj.tips.urlTipsFill = "preview/tips/tips_blue.png";
    obj.tips.urlTipsIcon = "preview/tips/tips_icon_2.png";
    obj.tips.height = 10.0;
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsShowObj", json);
    }
}
function delectTips() {
    if (mapIsInit) {
        unity.SendMessage("Map", "JsDelObj", "p01");
    }
}
function showModel() {
    var obj = { gid: "p02" };
    obj.name = "樟树2种";
    obj.oid = "m11";
    obj.coord = { lng_tx: 108.34968, lat_ty: 22.9236, dem: 1.3 };
    obj.model = { oid: "m12" };
    obj.model.scale = 10;
    if (mapIsInit) {
        var json = JSON.stringify(obj);
        unity.SendMessage("Map", "JsShowObj", json);
    }
}
function delectModel() {
    if (mapIsInit) {
        unity.SendMessage("Map", "JsDelObj", "p02");
    }
}
function openHttpLink(url)
{
	window.open(url);
}


///  以下代码用于解 WebGL 中文输入问题
var inputTextWin;
function showInputWin(json){
	var ar = JSON.parse(json);
	var title = ar[0];
	var orgText = ar[1];
	var html='<div style="background:rgba(0,0,0,0.4);width:100%;height:100%;position:fixed;top:0%;z-index:2147483647;">' + 
		'  <div style="position:relative;top:30%;" align="center" vertical-align="middle">' + 
		'    <div style="padding:10px; background:#e6e6e6; width:360px; outline:#0006 solid thick;outline-width: 1px;">' + 
		'      <div id="nativeInputDialogTitle" style="padding-bottom:10px;">'+title+'</div>' + 
		'      <textarea id="nativeInputDialogInput" style="width:100%;height:65px"></textarea>' + 
		'      <div style="margin-top:10px">' + 
		'        <input type="button" style="width:75px" value="OK" onclick="clickOk()" >' + 
		'        <input type="button" style="width:75px" value="Cancel" onclick ="delectIEMWin()">' + 
		'      </div>' + 
		'    </div>' + 
		'  </div>' + 
		'</div>';
	inputTextWin = document.createElement('div');
    inputTextWin.innerHTML = html;
	document.body.appendChild(inputTextWin);
    var inputField = document.getElementById("nativeInputDialogInput");
	inputField.value = orgText;
	inputTextWin.focus();
}
function clickOk(){
    var inputField = document.getElementById("nativeInputDialogInput");
	unity.SendMessage("JsManager", "changeInputField", inputField.value);
	delectIEMWin();
}
function delectIEMWin(){
	if(inputTextWin){
		inputTextWin.parentNode.removeChild(inputTextWin);
		inputTextWin=null;
		unity.SendMessage("JsManager", "enableUnityKeyInput","");
	}
}